package com.turing.data.scripts.zjtq.filter

import com.turing.common.util.FileUtils
import com.turing.common.util.Log
import com.turing.common.util.ObjectUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import com.turing.data.engine.utils.RegexExtract;

/**
 * 场景名称：追加提取单
 * 要素名称：金额大写
**/

class AmountInWords_30229_34_filter {

    static List< TaskElementVO.DrawResultVO > searchData(DependParamsVO dependParams){
        List<TaskElementVO.DrawResultVO > trueValueList = new ArrayList<>()

        if(trueValueList.size() == 0){
            String REGEX = /(?<=大写人民币【).{1,10}(?=】)/
            dependParams.JSONTextMap.forEach(
                    { childTaskSn, jsonText ->
                        RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueList)
                    }
            )
            if(trueValueList.size()>0){
                def textPathMap = dependParams.textPathMap
                def childTaskSn = trueValueList[0].childTaskSn
//                def rawPath = textPathMap.get(childTaskSn)
//                def rawText = FileUtils.loadJson(rawPath)
                def JSONTextMap = dependParams.JSONTextMap
//                def fileTextStr = ""
                def fileTextStr = JSONTextMap.get(childTaskSn)
//
                def rawPath0 = textPathMap.get(childTaskSn)
                def rawText0 = FileUtils.loadJson(rawPath0)

                def text = trueValueList[0].text
                def startOffset = 0
                def endOffset = 0

                TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(trueValueList[0])
                objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText0)
                trueValueList.clear()
                trueValueList.add(objectNew)
            }
        }

        if(trueValueList.size() == 0){
            TaskElementVO.DrawResultVO result = new TaskElementVO.DrawResultVO()
            result.text = ""
            trueValueList.add(result)
        }

        return trueValueList

    }

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        Log.info("AmountInWords_11437_34_filter_resultVOs="+resultVOs)
        Log.info("AmountInWords_11437_34_filter_dependParams="+dependParams)
        def JSONTextMap = dependParams.JSONTextMap
        def textPathMap = dependParams.textPathMap
        List<TaskElementVO.DrawResultVO > trueValueList = new ArrayList<>()
        try {
            if (resultVOs!=null && resultVOs.size()>0) {
                def resultVO = resultVOs.get(0)
                def text = resultVO.text
                def childTaskSn = resultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == "" || StringUtils.isEmpty(text)){
//                    trueValueList.add(resultVOs.get(0))
//                    return trueValueList
                    return searchData(resultVOs , dependParams)
                }
                def start = resultVO.start
                def end = resultVO.end

                def rawPath = textPathMap.get(resultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def fileTextStr = ""
                fileTextStr = JSONTextMap.get(childTaskSn)
//                resultVO.text = resultVO.text.replaceAll("。", "")
                resultVO.text = resultVO.text.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                resultVO.text = resultVO.text.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")
	if(!StringUtils.isEmpty(resultVO.businessValue)) {
		resultVO.businessValue = resultVO.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                	resultVO.businessValue = resultVO.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")
	}
                
	def expand = 0
                while(expand<8 && end + expand < rawText.length()){
                    def expandText = rawText.substring(start,end+expand)
                    if(expandText.endsWith("分")){
                        TaskElementVO.DrawResultVO drawResultNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                        drawResultNew = TextUtils.correctPositionInfoByOffset(drawResultNew, 0, expand, fileTextStr, rawText)
                        drawResultNew.text = expandText
                        drawResultNew.businessValue = expandText

 	drawResultNew.text = drawResultNew.text .replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                drawResultNew.text  =drawResultNew.text .replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")
	//if(!StringUtils.isEmpty(resultVO.businessValue)) {
		drawResultNew.businessValue = drawResultNew.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                	drawResultNew.businessValue = drawResultNew.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")
	//}

                        trueValueList.add(drawResultNew)
                        return trueValueList
                    }
                    expand++
                }
                if(!text.endsWith("元整")
                        && end +1 <= rawText.length() && rawText.substring(end,end+2) == "元整" ){
                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, 0, 2, fileTextStr, rawText)
                    ObjectNew.text = rawText.substring(start, end + 2 )

//                    ObjectNew.text = ObjectNew.text.replaceAll("。", "")
                    ObjectNew.text = ObjectNew.text.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.text = ObjectNew.text.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    ObjectNew.businessValue = rawText.substring(start, end + 1 )
//                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("。", "")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    trueValueList.add(ObjectNew)
                    return trueValueList
                }
                if(!text.endsWith("元")
                    && end +1 <= rawText.length() && rawText.substring(end,end+1) == "元" ){
                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, 0, 1, fileTextStr, rawText)
                    ObjectNew.text = rawText.substring(start, end + 1 )

//                    ObjectNew.text = ObjectNew.text.replaceAll("。", "")
                    ObjectNew.text = ObjectNew.text.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.text = ObjectNew.text.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    ObjectNew.businessValue = rawText.substring(start, end + 1 )
//                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("。", "")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    trueValueList.add(ObjectNew)
                    return trueValueList
                }
                if(!text.endsWith("整")
                        && end +1 <= rawText.length() && rawText.substring(end,end+1) == "整" ){
                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, 0, 1, fileTextStr, rawText)
                    ObjectNew.text = rawText.substring(start, end + 1 )

//                    ObjectNew.text = ObjectNew.text.replaceAll("。", "")
                    ObjectNew.text = ObjectNew.text.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.text = ObjectNew.text.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    ObjectNew.businessValue = rawText.substring(start, end + 1 )
//                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("。", "")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    trueValueList.add(ObjectNew)
                    return trueValueList
                }

                if (text.endsWith("。")) {
                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, 0, -1, fileTextStr, rawText)

                    ObjectNew.text = rawText.substring(start, end - 1 )

//                    ObjectNew.text = ObjectNew.text.replaceAll("。", "")
                    ObjectNew.text = ObjectNew.text.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.text = ObjectNew.text.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")

                    ObjectNew.businessValue = rawText.substring(start, end + 1 )
//                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("。", "")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("参", "叁").replaceAll("玫", "玖").replaceAll("盒", "叁").replaceAll("芳", "万").replaceAll("染", "柒").replaceAll("滤", "柒")
                    ObjectNew.businessValue = ObjectNew.businessValue.replaceAll("任", "仟").replaceAll("仔", "仟").replaceAll("仆", "仟")
                    trueValueList.add(ObjectNew)
                    return trueValueList
                }

                trueValueList.add(resultVOs.get(0))
                return trueValueList
            }

        }catch(Exception e){
            e.printStackTrace()
        }

        trueValueList.add(resultVOs.get(0))

        if(trueValueList.size() == 0){
            String REGEX = /大写人民币/
            dependParams.JSONTextMap.forEach(
                    { childTaskSn, jsonText ->
                        RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueList)
                    }
            )
            if(trueValueList.size()>0){
                 textPathMap = dependParams.textPathMap
                def childTaskSn = trueValueList[0].childTaskSn
//                def rawPath = textPathMap.get(childTaskSn)
//                def rawText = FileUtils.loadJson(rawPath)
                 JSONTextMap = dependParams.JSONTextMap
//                def fileTextStr = ""
                def fileTextStr = JSONTextMap.get(childTaskSn)
//
                def rawPath0 = textPathMap.get(childTaskSn)
                def rawText0 = FileUtils.loadJson(rawPath0)

                def text = trueValueList[0].text
                def startOffset = 0
                def endOffset = 0

                TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(trueValueList[0])
                objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText0)
                trueValueList.clear()
                trueValueList.add(objectNew)
                return trueValueList
            }
        }

        return trueValueList
    }

}
